﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using Geoinfor.Data;

namespace maintain_test.Geoinfor.CG.Beckett
{
    /// <summary>
    /// 通報類型
    /// </summary>
    public enum Kind
    {
        All = 0,
        Person = 1,
        Government = 2
    }
    /// <summary>
    /// 案件狀態
    /// </summary>
    public enum Status
    {
        On = 0,
        Ing = 1,
        Off = 2
    }

    public class Announce
    {
        private DataSet _dateSet = null;
        private DAC dac = new DAC();
        private StringBuilder Sql = new StringBuilder();
        private string _id;
        private Kind _kind;
        private string _startDate;
        private string _place;
        private string _nextPlace;
        private string _man;
        private string _email;
        private string _tel;
        private string _note;
        private Status _status;
        private string _endDate;
        private string _endNote;
        private string _endMoney;
        private string _city;
        private string _roadClass;
        private string _powerUnit;

        /// <summary>
        /// 流水編號
        /// </summary>
        public string Id
        {
            get { return _id; }
            set { _id = value; }
        }

        /// <summary>
        /// 通報日期
        /// </summary>
        public string StartDate
        {
            get { return _startDate; }
            set { _startDate = value; }
        }

        /// <summary>
        /// 通報類別
        /// </summary>
        public Kind kind
        {
            get { return _kind; }
            set { _kind = value; }
        }
        /// <summary>
        /// 損壞地點
        /// </summary>
        public string Place
        {
            get { return _place; }
            set { _place = value; }
        }
        /// <summary>
        /// 鄰近地點
        /// </summary>
        public string NextPlace
        {
            get { return _nextPlace; }
            set { _nextPlace = value; }
        }
        /// <summary>
        /// 通報人姓名
        /// </summary>
        public string Man
        {
            get { return _man; }
            set { _man = value; }
        }
        /// <summary>
        /// 電子信箱
        /// </summary>
        public string Email
        {
            get { return _email; }
            set { _email = value; }
        }
        /// <summary>
        /// 電話
        /// </summary>
        public string Tel
        {
            get { return _tel; }
            set { _tel = value; }
        }
        /// <summary>
        /// 狀況描述
        /// </summary>
        public string Note
        {
            get { return _note; }
            set { _note = value; }
        }
        /// <summary>
        /// 案件狀況
        /// </summary>
        public Status status
        {
            get { return _status; }
            set { _status = value; }
        }
        /// <summary>
        /// 修復日期
        /// </summary>
        public string EndDate
        {
            get { return _endDate; }
            set { _endDate = value; }
        }
        /// <summary>
        /// 修復描述
        /// </summary>
        public string EndNote
        {
            get { return _endNote; }
            set { _endNote = value; }
        }
        /// <summary>
        /// 經費
        /// </summary>
        public string EndMoney
        {
            get { return _endMoney; }
            set { _endMoney = value; }
        }
        /// <summary>
        /// 鄉鎮市
        /// </summary>
        public string City
        {
            get { return _city; }
            set { _city = value; }
        }
        /// <summary>
        /// 損壞類型
        /// </summary>
        public string RoadClass
        {
            get { return _roadClass; }
            set { _roadClass = value; }
        }
        /// <summary>
        /// 權責單位
        /// </summary>
        public string PowerUnit
        {
            get { return _powerUnit; }
            set { _powerUnit = value; }
        }


        /// <summary>
        /// 初始化Announce
        /// </summary>
        public Announce()
        {

        }

        /// <summary>
        /// 初始化Announce
        /// </summary>
        /// <param name="_k">通報類別</param>
        /// <param name="startDate">通報日期</param>
        /// <param name="place">損壞地點</param>
        /// <param name="nextPlace">鄰近地點</param>
        /// <param name="man">通報人姓名</param>
        /// <param name="email">電子信箱</param>
        /// <param name="tel">電話</param>
        /// <param name="note">狀況描述</param>
        /// <param name="_s">狀態</param>
        /// <param name="endDate">修復日期</param>
        /// <param name="endNote">修復描述</param>
        /// <param name="endMoney">經費</param>
        /// <param name="city">鄉鎮市</param>
        /// <param name="roadClass">損壞類型</param>
        /// <param name="powerUnit">權責單位</param>
        public Announce(Kind _k, string startDate, string place, string nextPlace,
                        string man, string email, string tel, string note, Status _s,
                        string endDate, string endNote, string endMoney, string city,
                        string roadClass, string powerUnit)
        {

            Id = GetMaxIdAndPopOne("APPNO", "MTAnnounce");
            kind = _k;
            StartDate = startDate;
            Place = place;
            NextPlace = nextPlace;
            Man = man;
            Email = email;
            Tel = tel;
            Note = note;
            status = _s;
            EndDate = endDate;
            EndNote = endNote;
            EndMoney = endMoney;
            City = city;
            RoadClass = roadClass;
            PowerUnit = powerUnit;
        }


        /// <summary>
        /// 初始化Announce
        /// </summary>
        /// <param name="id">流水號</param>
        /// <param name="_k">通報類別</param>
        /// <param name="startDate">通報日期</param>
        /// <param name="place">損壞地點</param>
        /// <param name="nextPlace">鄰近地點</param>
        /// <param name="man">通報人姓名</param>
        /// <param name="email">電子信箱</param>
        /// <param name="tel">電話</param>
        /// <param name="note">狀況描述</param>
        /// <param name="_s">狀態</param>
        /// <param name="endDate">修復日期</param>
        /// <param name="endNote">修復描述</param>
        /// <param name="endMoney">經費</param>
        /// <param name="city">鄉鎮市</param>
        /// <param name="roadClass">損壞類型</param>
        /// <param name="powerUnit">權責單位</param>
        public Announce(string id, Kind _k, string startDate, string place, string nextPlace,
                        string man, string email, string tel, string note, Status _s,
                        string endDate, string endNote, string endMoney, string city,
                        string roadClass, string powerUnit)
        {
            Id = id;
            kind = _k;
            StartDate = startDate;
            Place = place;
            NextPlace = nextPlace;
            Man = man;
            Email = email;
            Tel = tel;
            Note = note;
            status = _s;
            EndDate = endDate;
            EndNote = endNote;
            EndMoney = endMoney;
            City = city;
            RoadClass = roadClass;
            PowerUnit = powerUnit;
        }

        /// <summary>
        /// 新增Announce
        /// </summary>
        /// <returns></returns>
        public int Insert()
        {
            Sql.Length = 0;
            Sql.Append("INSERT INTO MTAnnounce");
            Sql.Append("(APPNO,KIND,APPDATE,APPPLACE,APPMAN,APPEMAIL,APPTEL,APPNOTE,STATUS,REDATE,REMONEY,ENDNOTE,NEXTPLACE,CITY,ROADCLASS,POWERUNIT) VALUES (");
            Sql.Append("'" + this.Id + "' , ");
            Sql.Append("'" + (int)this.kind + "' , ");
            Sql.Append("'" + this.StartDate + "' , ");
            Sql.Append("'" + this.Place + "' , ");
            Sql.Append("'" + this.Man + "' , ");
            Sql.Append("'" + this.Email + "' , ");
            Sql.Append("'" + this.Tel + "' , ");
            Sql.Append("'" + this.Note + "' , ");
            Sql.Append("'" + (int)this.status + "' , ");
            Sql.Append("'" + this.EndDate + "' , ");
            Sql.Append("'" + this.EndMoney + "' , ");
            Sql.Append("'" + this.EndNote + "' , ");
            Sql.Append("'" + this.NextPlace + "' , ");
            Sql.Append("'" + this.City + "' , ");
            Sql.Append("'" + this.RoadClass + "' , ");
            Sql.Append("'" + this.PowerUnit + "' )");

            return dac.ExecuteSQL(Sql.ToString());
        }

        public int Delete(string id)
        {
            Sql.Length = 0;
            Sql.Append(" delete from MTAnnounce ");
            Sql.Append(" where APPNO = ");
            Sql.Append("'");
            Sql.Append(id);
            Sql.Append("'");

            return dac.ExecuteSQL(Sql.ToString());
        }

        public DataSet GetAnnounceList(Kind k)
        {
            _dateSet = null;
            Sql.Length = 0;
            Sql.Append(" select * from MTAnnounce where 1=1 ");

            if ((int)k > 0)
            {
                Sql.Append(" and kind = '" + ((int)k).ToString() + "' ");
            }
            Sql.Append(" ORDER BY APPNO DESC");
            return _dateSet = dac.QueryDataSet(Sql.ToString(), "Announce");
        }


        public Announce GetAnnounceById(string id)
        {

            Sql.Length = 0;
            Sql.Append(" SELECT APPNO,KIND,APPDATE,APPPLACE,APPMAN,APPEMAIL,APPTEL,APPNOTE,STATUS,REDATE,REMONEY,ENDNOTE,NEXTPLACE,CITY,ROADCLASS,POWERUNIT FROM MTAnnounce ");
            Sql.Append(" where APPNO = ");
            Sql.Append("'");
            Sql.Append(id);
            Sql.Append("'");

            DataSet ds = dac.QueryDataSet(Sql.ToString(), "Announce");

            Announce announce = null;
            //DateSet表格裡面沒有資料
            if (ds.Tables.Count == 1 && ds.Tables[0].Rows.Count == 0)
            {
                return announce;
            }
            else
            {
                announce = new Announce(ds.Tables["Announce"].Rows[0][0].ToString(),
                                      (Kind)Enum.Parse(typeof(Kind), ds.Tables["Announce"].Rows[0][1].ToString()),
                                      ds.Tables["Announce"].Rows[0][2].ToString(),
                                      ds.Tables["Announce"].Rows[0][3].ToString(),
                                      ds.Tables["Announce"].Rows[0][4].ToString(),
                                      ds.Tables["Announce"].Rows[0][5].ToString(),
                                      ds.Tables["Announce"].Rows[0][6].ToString(),
                                      ds.Tables["Announce"].Rows[0][7].ToString(),
                                      ds.Tables["Announce"].Rows[0][8].ToString(),
                                      (Status)Enum.Parse(typeof(Status), ds.Tables["Announce"].Rows[0][9].ToString()),
                                      ds.Tables["Announce"].Rows[0][10].ToString(),
                                      ds.Tables["Announce"].Rows[0][11].ToString(),
                                      ds.Tables["Announce"].Rows[0][12].ToString(),
                                      ds.Tables["Announce"].Rows[0][13].ToString(),
                                      ds.Tables["Announce"].Rows[0][14].ToString(),
                                      ds.Tables["Announce"].Rows[0][15].ToString());
            }
            return announce;
        }

        public string GetMaxIdAndPopOne(string sSYS_ID, string sTableName)
        {
            string sResult = string.Empty;
            Sql.Length = 0;
            Sql.Append("select MAX(" + sSYS_ID + ")");
            Sql.Append(" from " + sTableName);
            sResult = dac.SelectOneField(Sql.ToString());
            if (sResult == "")
            {
                sResult = "0000";
            }
            else
            {
                sResult = sResult.Substring(4, 4);
            }
            int no = Int32.Parse(sResult) + 1;
            StringBuilder sno = new StringBuilder();
            sno.Length = 0;
            sno.Append(DateTime.Now.Year.ToString("D4"));
            sno.Append(no.ToString("D4"));
            return sno.ToString();
        }
    }
}
